package org.lanqiao.service.impl;

import org.lanqiao.dao.UserDao;
import org.lanqiao.dao.impl.UserDaoImpl;
import org.lanqiao.entity.User;
import org.lanqiao.service.UserService;

import java.util.List;

public class UserServiceImpl implements UserService {
    private UserDao dao = new UserDaoImpl();

    @Override
    public boolean addUser(User user) {
        boolean exist = dao.isExistById(user.getId());
        int row = -1;
        if (exist) {
            System.out.println("存在，不能添加！");
            return false;
        } else {
            row = dao.addUser(user);
        }
        return row == 1 ? true : false;
    }

    @Override
    public boolean updUser(User user) {
        boolean exist = dao.isExistById(user.getId());
        int row = -1;
        if (exist) {
            row = dao.updUser(user);
        } else {
            System.out.println("不存在，不能修改！");
            return false;
        }
        return row == 1 ? true : false;
    }

    @Override
    public boolean delUser(Integer id) {
        boolean exist = dao.isExistById(id);
        int row = -1;
        if (exist) {
            row = dao.delUser(id);
        } else {
            System.out.println("不存在，不能删除！");
            return false;
        }
        return row == 1 ? true : false;
    }

    @Override
    public List<User> findAll() {
        return dao.findAll();
    }

    @Override
    public User findById(Integer id) {
        return dao.findById(id);
    }

    @Override
    public boolean isExistById(Integer id) {
        return dao.isExistById(id);
    }
}
